package com.wfs.suanfa.leet;

/**
 * 二分法排查找
 */
public class BinarySearchTest {
    public static void main(String[] args) {

        int[] arr=new int[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(binarySearch(arr,10));
    }

    public  static  int binarySearch(int[] arr,int data){
        int hight=arr.length-1;
        int low=0;

        while (hight>=low){
            int mid=low+(hight-low)/2;
            int temp = arr[mid];
            if (temp<data){
                low=mid+1;
            }else if (temp==data){
                return mid;
            }else {
                hight=mid-1;
            }
        }

        return -1;
    }
}
